import { ref, reactive, computed } from "vue"
import { toast } from "~/composables/util"
import { useRoute,useRouter } from 'vue-router'
import { getStorage} from '~/composables/storage'
import { useStore } from 'vuex'




// 查看详情
export function useDetail(opt = {}) {
    const detailValue = ref({})
    const route = useRoute()
    const router = useRouter()
    const isShow = route.query.isShow
    function getList (){
      let strQuery = route.query.id
      if(route.query.userid){
        strQuery= strQuery+'/'+route.query.userid
      }
      if(route.query.usertype){
        strQuery= strQuery+'/'+route.query.usertype
      }
      if(route.query.placeType){
        strQuery= strQuery+'/'+route.query.placeType
      }
        if(strQuery){
            opt.getDetail(strQuery).then(res=>{
            if (opt.onGetListSuccess && typeof opt.onGetListSuccess == "function") {
                opt.onGetListSuccess(res)
            } else {
              if(res.code == '0000'){
                  detailValue.value = res.data.result
              }
            }
            })
        }
    }

    if(!opt.isFirst){
      getList()
    }

   

    return {
        detailValue,
        getList,
        isShow
    }
}

// 跳转等

// 查看详情
export function useGoFn(opt = {}) {
    const route = useRoute()
    const router = useRouter()
    const store = useStore()

    const tapGoSass =(item)=>{
        router.push({
          name: 'sassDetail',
          query: {
            id: item.saasServiceId,
          }
        })
    }

    const tapGoqiYE =(item,isShow = 1)=>{
        router.push({
            name: 'firmDetail',
            query: {
              id: item.id,
              isShow
            }
          })
    }

    const tapGoChanYE =(item)=>{
        router.push({
            name: 'gameDetail',
            query: {
              id: item.orgId
            }
          })
    }

    const tapGoTeacher =(item)=>{
        router.push({
            name: 'teacherDetail',
            query: {
              id: item.industryTutorId
            }
          })
    }

    // 直播预告
    const tapGoNotice =(item)=>{
      router.push({
          name: 'noticeDetail',
          query: {
            id: item.liveId
          }
        })
  }
    // 直播精品
    const tapGoLive =(item)=>{

      const token = getStorage('token')
      if(!token){
        toast('请先登录','warning')
        return router.push('/login')
      }
      router.push({
          name: 'liveRoom',
          query: {
            id: item.courseId,
            userid:store.getters.userID || null,
            usertype:store.state.userType || null,
            placeType:1
          }
        })
  }

  // 赛事
  function tapEvent(i){
    router.push({
        name: 'raceControlDetail',
        query: {
          id: i.eventId
        }
      })
  }


  // 培训详情
  function tapTalentsDetail(i){
    router.push({
        name: 'talentsDetail',
        query: {
          id: i.trainingId,
          userid:store.getters.userID || null,
          usertype:store.state.userType || null,
          // placeType:4
        }
      })
  }

  // 职位详情
  function tapJobDetail(i){
    router.push({
        name: 'jobDetail',
        query: {
          id: i.recruitmentId
        }
      })
  }

  // 咨询详情
  function tapJournalismDetail(i){
    router.push({
        name: 'journalismDetail',
        query: {
          id: i.infoId
        }
      })
  }


    // 商品详情
    function tapShopDetail(i){

      router.push({
          name: 'shopDetail',
          query: {
            id: i.goodsId,
            userid:store.getters.userID || null,
            usertype:store.state.userType || null
          }
        })
    }

    return {
        tapGoSass,
        tapGoqiYE,
        tapGoChanYE,
        tapGoTeacher,
        tapGoNotice,
        tapGoLive,
        tapEvent,
        tapTalentsDetail,
        tapJobDetail,
        tapJournalismDetail,
        tapShopDetail
    }
}



